<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var constructMaximumBinaryTree = function (nums) {
        function buildTree(arr) {
          if (!arr.length) return
          let maxValue = -Infinity
          let maxIndex = -1
          for (let i = 0; i < arr.length; i++) {
            if (arr[i] > maxValue) {
              maxValue = arr[i]
              maxIndex = i
            }
          }
          let node = new TreeNode(maxValue)
          node.left = buildTree(arr.slice(0, maxIndex))
          node.right = buildTree(arr.slice(maxIndex + 1))
          return node
        }
        return buildTree(nums)
      }
    </script>
  </body>
</html>
